import { ajaxService } from '@/utils/request';
import { getToken } from '@/utils/auth';
import axios from 'axios';
import {
  PostClientOrder,
  GetClientOrder,
  addClientOrder,
  DeleteCustomerMessege,
} from './type';

const env = import.meta.env;
let baseURL = env.VITE_ORDER_URL;

/* 客户订单   */

export interface getOrderFormStatusOn {
  id: number;
}

// 获取已经打开状态那个模板
export function getOrderFormStatusOn(params: getOrderFormStatusOn) {
  return ajaxService({
    url: '/order/get',
    method: 'get',
    params,
    baseURL,
  });
}

// 获取客户信息
export interface getCustomerMessege {
  customerId: number;
}

export function getCustomerMessege(params: getCustomerMessege) {
  return ajaxService({
    url: '/order/sub/getCustomerMessege',
    method: 'get',
    params,
    baseURL,
  });
}

// 删除订单
export function DeleteClientOrder(params: DeleteCustomerMessege) {
  return ajaxService({
    url: '/order/del',
    method: 'delete',
    params,
    baseURL,
  });
}

// 添加订单
export function addClientOrder(data: addClientOrder) {
  return ajaxService({
    url: '/order/add',
    method: 'post',
    data,
    baseURL,
  });
}

// 添加订单
export function toClientOrder(data: addClientOrder) {
  return ajaxService({
    url: '/order/demand/to/order',
    method: 'post',
    data,
    baseURL,
  });
}

// 获取订单列表
export function GetClientOrder(params: any) {
  return ajaxService({
    url: '/order/search',
    params,
    method: 'get',
    baseURL,
  });
}

// 获取单个订单详情
export function GetClientOrderDetail(params: GetClientOrder) {
  return ajaxService({
    url: '/order/getOne',
    method: 'get',
    params,
    baseURL,
  });
}

// 获取单个订单详情(执行订单)
export function GetOrderDetail(params: GetClientOrder) {
  return ajaxService({
    url: '/order/get',
    method: 'get',
    params,
    baseURL,
  });
}

// 修改订单详情
export function PostClientOrder(data: PostClientOrder) {
  return ajaxService({
    url: '/order/search',
    data,
    method: 'post',
    baseURL,
  });
}

// 获取客户
export function GetCustomer() {
  return ajaxService({
    url: '/contract/subs/getAll',
    method: 'get',
    baseURL,
  });
}

// 获取订单档案
export function GetOrderfiles(params: any) {
  return ajaxService({
    url: '/order/files',
    method: 'get',
    params,
    baseURL,
  });
}

// 获取表头
export function GetHeaderSettings(params: any) {
  return ajaxService({
    url: '/header/order',
    method: 'get',
    params,
    baseURL,
  });
}

// 设置表头
export function putHeaderSettings(data: any) {
  return ajaxService({
    url: '/header/order',
    method: 'put',
    data,
    baseURL,
  });
}

// 修改销售订单
export function putClientOrder(data: any) {
  return ajaxService({
    url: '/order/mod',
    method: 'put',
    data,
    baseURL,
  });
}

export async function GetOrderDeriveExcel(name?: string) {
  const res: any = await axios({
    method: 'get',
    url: '/order/derive',
    baseURL,
    headers: {
      'Content-Type': 'multipart/form-data;charset=UTF-8',
      'Authorization': `${getToken()}`,
    },
    responseType: 'blob',
    // data: file,
  });
  const file = new Blob([res.data], { type: 'application/xlsx' });
  const changeUrl = window.webkitURL.createObjectURL(file);
  let downloadElement: any = document.createElement('a');
  downloadElement.download = (name || 'test') + '.xlsx';
  downloadElement.href = changeUrl;
  downloadElement.click();
  downloadElement = undefined;
  window.URL.revokeObjectURL(changeUrl);
}

// 新增或修改订单编号设置
export function postOrderNumSet(data: any) {
  return ajaxService({
    url: '/order/numSet/add',
    method: 'post',
    data,
    baseURL,
  });
}

// 修改出库占用数量
export function putPoductOccupied(data: any) {
  return ajaxService({
    url: '/order/product/occupied',
    method: 'PUT',
    data,
    baseURL,
  });
}

// 修改出库占用数量
export function getPoductOccupiedAll(params: any) {
  return ajaxService({
    url: '/order/product/get/occupied',
    method: 'get',
    params,
    baseURL,
  });
}

// 批量确定需求订单状态
export function modState(data: any[]) {
  return ajaxService({
    url: '/order/mod/state',
    method: 'put',
    data,
    baseURL,
  });
}

// 新建审批流程
export function addFlowProcess(data: number[]) {
  return ajaxService({
    url: '/order/flow/process',
    method: 'post',
    data,
    baseURL,
  });
}

// 编辑审批流程
export function putFlowProcess(data: number[]) {
  return ajaxService({
    url: '/order/flow/process',
    method: 'put',
    data,
    baseURL,
  });
}

// 编辑审批流程
export function delFlowProcess(params: { id: number }) {
  return ajaxService({
    url: '/order/flow/process',
    method: 'delete',
    params,
    baseURL,
  });
}

// 获取审批列表
export function getFlowProcessAll(params: number[]) {
  return ajaxService({
    url: '/order/flow/process/getAll',
    method: 'get',
    params,
    baseURL,
  });
}

// 获取审批列表
export function getoccupiedAll(params: number[]) {
  return ajaxService({
    url: '/order/product/get/occupied/all',
    method: 'get',
    params,
    baseURL,
  });
}
